<template>
  <el-upload
    ref="upload"
    name="avatar"
    :headers="{'x-access-token': token}"
    action="/api/users/upload"
    :auto-upload="false"
    :on-success="handleSuccess"
  >
    <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
  </el-upload>
</template>

<script>
export default {
  computed: {
    token() {
      return localStorage.getItem('x-access-token')
    }
  },

  methods: {
    handleSuccess(res) {
      if (res.ret) {
        this.$emit('update:filename', res.data.filename)
      } else {
        this.$message({
          message: res.data.msg,
          center: true
        })
      }
    }
  },
}
</script>

<style lang='stylus' scoped>

</style>